﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace JoomlaXMLEditor
{
    // edytor dodawania języka
    public partial class TemplateLanguageEditor : Form
    {
        // uchwyt do okna
        private TemplateLanguages parent;
        // indeks przy edycji
        private int finalIndex;
        // konstruktor
        public TemplateLanguageEditor(TemplateLanguages parent, int index)
        {
            InitializeComponent();

            this.parent = parent;
            this.finalIndex = index;

            if (index != -1)
            {
                this.textBox1.Text = this.parent.listBox1.Items[index].ToString();
                if (this.parent.languages[index].Attributes["tag"] != null)
                {
                    this.textBox2.Text = this.parent.languages[index].Attributes["tag"].Value;
                }
            }
        }
        // zamykanie
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // zapisywanie
        private void button1_Click(object sender, EventArgs e)
        {
            if (Validator.ININame(textBox1.Text) && Validator.LangName(textBox2.Text))
            {
                if (finalIndex != -1)
                {
                    this.parent.listBox1.Items[finalIndex] = this.textBox1.Text;
                    this.parent.languages[finalIndex].Attributes["tag"].Value = this.textBox2.Text;
                    this.parent.languages[finalIndex].FirstChild.Value = this.textBox1.Text;
                }
                else
                {
                    this.parent.listBox1.Items.Add(this.textBox1.Text);
                    XmlElement node = this.parent.parent.actualFile.CreateElement("language");
                    XmlText textNode = this.parent.parent.actualFile.CreateTextNode(this.textBox1.Text);
                    node.AppendChild(textNode);
                    XmlAttribute attr = this.parent.parent.actualFile.CreateAttribute("tag");
                    attr.Value = this.textBox2.Text;
                    node.Attributes.Append(attr);
                    this.parent.languages.Add(node);
                }

                this.Close();
            }
            else
            {
                MessageBox.Show("Proszę sprawdzić format nazwy i oznaczenia pliku językowego");
            }
        }
    }
}
